package offerbook;

import java.util.Stack;

/**
 * @date 2020/3/7 23:18
 */
public class Code07_StackToDeque {
    //pushStack栈顶始终保持着最后入栈元素
    Stack<Integer> pushStack = new Stack<Integer>();
    //popStack栈顶始终保持着最先入栈元素
    Stack<Integer> popStack = new Stack<Integer>();

    //offer
    public void addLast(int node) {
        pushStack.push(node);
    }

    /**
     * pushStack栈顶始终保持着最后插入的元素
     */
    public int pollLast(){
        if (isEmpty()) throw new RuntimeException("empty");
        dao2();
        return pushStack.pop();
    }

    /**
     * 向队列头部添加，始终保持最新，所以直接加入到pop栈顶
     */
    public void addFirst(int node){
        popStack.add(node);
    }
    public int pollFirst() {
        if (isEmpty()) throw new RuntimeException("empty");
        dao1();
        return popStack.pop();
    }

    public void dao1() {
        if (!popStack.isEmpty()) return;
        while (!pushStack.isEmpty()) {
            popStack.push(pushStack.pop());
        }
    }

    public void dao2() {
        if (!pushStack.isEmpty()) return;
        while (!popStack.isEmpty()) {
            pushStack.push(popStack.pop());
        }
    }
    public boolean isEmpty () {
        return pushStack.isEmpty() && popStack.isEmpty();
    }
}